package com.ebelter.btcomlib.utils;


import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.text.format.DateUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
@SuppressLint("SimpleDateFormat")
public class TimeUtils {

    private static final String TAG = "TimeUtils";


    private static Date curDate = new Date(System.currentTimeMillis());//获取当前时间


    /**
     *国际化时间 年月日
     */
    public static String formatDate_nyr(Context context,long when) {
        int format_flags = android.text.format.DateUtils.FORMAT_SHOW_DATE| android.text.format.DateUtils.FORMAT_SHOW_YEAR;
        return android.text.format.DateUtils.formatDateTime(context, when, format_flags);
    }

    /**
     *国际化时间 月日
     */
    public static String formatDate_yr(Context context,long when) {
        int format_flags = android.text.format.DateUtils.FORMAT_SHOW_DATE| DateUtils.FORMAT_NO_YEAR;
        return DateUtils.formatDateTime(context, when, format_flags);
    }

    /**
     *国际化时间 时分
     */
    public static String formatTime_shf(Context context,long when) {
        int format_flags = android.text.format.DateUtils.FORMAT_NO_NOON_MIDNIGHT
                | android.text.format.DateUtils.FORMAT_ABBREV_ALL
                | android.text.format.DateUtils.FORMAT_SHOW_TIME;
        return DateUtils.formatDateTime(context, when, format_flags);
    }

    /**
     * 获取今天的时间
     * @param amount 今天的偏移量
     */
    public static String getTodayZeroTime(int amount,boolean isAppend0){
        SimpleDateFormat formatter3=new SimpleDateFormat("yyyy-MM-dd");
        Date date=new Date();//取时间
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE,amount);//把日期往后增加一天.整数往后推,负数往前移动
        date=calendar.getTime(); //这个时间就是日期往后推一天的结果
        String dateString = formatter3.format(date);
        if (isAppend0){
            dateString = dateString + " 00:00:00";
        }
        LogUtils.i("TimeUtils","amount ="+amount+ "----"+dateString+"---isAppend0 = "+isAppend0);
        return dateString;
    }

    public static Date getCurDate(){
        curDate.setTime(System.currentTimeMillis());
        return curDate;
    }

    //获取当前时间
    public static String getCurrentTime1(){
        SimpleDateFormat formatter1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        curDate.setTime(System.currentTimeMillis());
        return formatter1.format(curDate);
    }


    //获取当前时间一定偏移量的时间
    public static String getCurrentTime1(long offset){
        long t = System.currentTimeMillis()+offset;
        return formatTime1(t);
    }


    public static String formatTime1(long time){
        SimpleDateFormat formatter1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        curDate.setTime(time);
        return formatter1.format(curDate);
    }


    //获取当前时间
    public static String getCurrentTime2(){
        SimpleDateFormat formatter2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        curDate.setTime(System.currentTimeMillis());
        return formatter2.format(curDate);
    }
    public static String getCurrentTime3(){
        SimpleDateFormat formatter3=new SimpleDateFormat("yyyy-MM-dd");
        curDate.setTime(System.currentTimeMillis());
        return formatter3.format(curDate);
    }

    public static String formatTime2(long time){
        SimpleDateFormat formatter2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        curDate.setTime(time);
        return formatter2.format(curDate);
    }
    public static String formatTime3(long time){
        SimpleDateFormat formatter3=new SimpleDateFormat("yyyy-MM-dd");
        curDate.setTime(time);
        return formatter3.format(curDate);
    }

    public static String formatTime20(long time){
        SimpleDateFormat formatter1=new SimpleDateFormat("HH:mm:ss");
        curDate.setTime(time);
        return formatter1.format(curDate);
    }


    //年月日
    public static String formatDateStampString0(Context context,long when) {
        int format_flags = DateUtils.FORMAT_SHOW_DATE| DateUtils.FORMAT_SHOW_YEAR;
        return DateUtils.formatDateTime(context, when, format_flags);
    }

    //月日
    public static String formatDateStampString1(Context context,long when) {
        int format_flags = DateUtils.FORMAT_SHOW_DATE| DateUtils.FORMAT_NO_YEAR;
        return DateUtils.formatDateTime(context, when, format_flags);
    }


    //时分
    public static String formatDateStampString2(Context context,long when) {
        int format_flags = DateUtils.FORMAT_NO_NOON_MIDNIGHT
                | DateUtils.FORMAT_ABBREV_ALL
                | DateUtils.FORMAT_SHOW_TIME;
        return DateUtils.formatDateTime(context, when, format_flags);
    }

    //解析formatter1时间//2006-08-30 00:00
    public static Date parseFormatter1Time(String timeStr){

        try {
            SimpleDateFormat formatter1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return formatter1.parse(timeStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return new Date();
        }

    }

    public static boolean judgingTimeFormat(String timeStr,String format){

        try {
            SimpleDateFormat formatter1=new SimpleDateFormat(format);
            formatter1.parse(timeStr);
            return true;
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }

    }
    public static String formatTime17(long time){
        SimpleDateFormat formatter17=new SimpleDateFormat("yyMMddHH");
        curDate.setTime(time);
        return formatter17.format(curDate);
    }

    public static Date parseFormatter17Time(String timeStr){

        try {
            SimpleDateFormat formatter17=new SimpleDateFormat("yyMMddHH");
            return formatter17.parse(timeStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return new Date();
        }

    }

    public static Date parseTimeFormatter(String timeStr,String pattern){

        try {
            SimpleDateFormat formatter17=new SimpleDateFormat(pattern);
            return formatter17.parse(timeStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return new Date();
        }

    }


    public static Date parseFormatter37Time(String timeStr){

        try {
            SimpleDateFormat formatter17=new SimpleDateFormat("yyMMddHHmmss");
            return formatter17.parse(timeStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return new Date();
        }

    }
    //解析formatter1时间
    public static Date parseFormatter6Time(String timeStr){

        try {
            SimpleDateFormat formatter6=new SimpleDateFormat("yyyy-MM-dd HH:mm");
            return formatter6.parse(timeStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return new Date();
        }

    }

    //解析formatter1时间
    public static String formatter15Time(long time){
        SimpleDateFormat formatter17=new SimpleDateFormat("yyMMddHH");
        curDate.setTime(time);
        return formatter17.format(curDate);
    }

    //解析formatter1时间
    public static Date parseFormatter15Time(String timeStr){
        try {
            SimpleDateFormat formatter15=new SimpleDateFormat("yyMMddHHmm");
            return formatter15.parse(timeStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return new Date();
        }
    }

    //解析formatter1时间
    public static Date parseFormatter91Time(String timeStr){

        try {
            SimpleDateFormat formatter91=new SimpleDateFormat("yyyy-MM");
            return formatter91.parse(timeStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return new Date();
        }

    }
    //解析formatter1时间
    public static Date parseFormatter3Time(String timeStr){

        try {
            @SuppressLint("SimpleDateFormat") SimpleDateFormat formatter3=new SimpleDateFormat("yyyy-MM-dd");
            return formatter3.parse(timeStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return new Date();
        }

    }
    public static String formatTime5(long time){
        SimpleDateFormat formatter5=new SimpleDateFormat("yyyy-M-d");
        curDate.setTime(time);
        return formatter5.format(curDate);
    }

    public static String formatTime9(long time){
        SimpleDateFormat formatter9=new SimpleDateFormat("yyyy-M");
        curDate.setTime(time);
        return formatter9.format(curDate);
    }
    public static String formatTime91(long time){
        SimpleDateFormat formatter91=new SimpleDateFormat("yyyy-MM");
        curDate.setTime(time);
        return formatter91.format(curDate);
    }
    public static String formatTime10(long time){
        SimpleDateFormat formatter10=new SimpleDateFormat("yyyy");
        curDate.setTime(time);
        return formatter10.format(curDate);
    }
    public static String formatTime4(long time){
        SimpleDateFormat formatter4=new SimpleDateFormat("HH:mm");
        curDate.setTime(time);
        return formatter4.format(curDate);
    }
    public static String formatTime11(long time){
        SimpleDateFormat formatter11=new SimpleDateFormat("M-d");
        curDate.setTime(time);
        return formatter11.format(curDate);
    }
    public static String formatTime12(long time){
        SimpleDateFormat formatter12=new SimpleDateFormat("M");
        curDate.setTime(time);
        return formatter12.format(curDate);
    }
    public static String formatTime14(long time){
        SimpleDateFormat formatter14=new SimpleDateFormat("yyMMdd");
        curDate.setTime(time);
        return formatter14.format(curDate);
    }
    public static String formatTime13(long time){
        SimpleDateFormat formatter13=new SimpleDateFormat("HH:mm:ss");
        curDate.setTime(time);
        return formatter13.format(curDate);
    }
    public static String formatTime16(long time){
        SimpleDateFormat formatter16=new SimpleDateFormat("H");
        curDate.setTime(time);
        return formatter16.format(curDate);
    }

    public static String formatTimeByMyselt(SimpleDateFormat simpleDateFormat,long time){
        curDate.setTime(time);
        return simpleDateFormat.format(curDate);
    }

    public static String formatTime21(long time){
        SimpleDateFormat formatter1=new SimpleDateFormat("MM-dd HH:mm:ss");
        curDate.setTime(time);
        return formatter1.format(curDate);
    }

    public static String formatTime22(long time){
        SimpleDateFormat formatter1=new SimpleDateFormat("MM-dd HH:mm");
        curDate.setTime(time);
        return formatter1.format(curDate);
    }

    /**
     *
     * @param timeStr 1970-01-01 08:00:00 时间格式永远是这样的
     * @param fatmat 1年月日 2时分
     */
    public static String timeFatmat(String timeStr, int fatmat){
        if (TextUtils.isEmpty(timeStr)){
            return "";
        }
        if (timeStr.length()==10){
            //说明时间可能是1970-01-01
            timeStr=timeStr+" 00:00:00";
        }
        String result="";
        if (timeStr.length()!=19){
            LogUtils.i(TAG,timeStr);
            return result;//说明传进来的时间格式是不正确的
        }
        //String string="1994-16-18 18:19:32";
        int year=NumUtils.string2Int(timeStr.substring(0,4));
        int month=NumUtils.string2Int(timeStr.substring(5,7));
        int day=NumUtils.string2Int(timeStr.substring(8,10));
        int h=NumUtils.string2Int(timeStr.substring(11,13));
        int m=NumUtils.string2Int(timeStr.substring(14,16));
        int s=NumUtils.string2Int(timeStr.substring(17,19));
        if (fatmat==1){
            result = SpecialText.formatDateStampString1(new Date(year-1900,month-1,day,h,m,s).getTime());
        }else if (fatmat==2){
            result=SpecialText.formatDateStampString2(new Date(year-1900,month-1,day,h,m,s).getTime());
        }else if (fatmat==3){
            result=SpecialText.formatDateStampString3(new Date(year-1900,month-1,day,h,m,s).getTime());
        }else if (fatmat==0){
            result = SpecialText.formatDateStampString0(new Date(year-1900,month-1,day,h,m,s).getTime());
        }
        LogUtils.i("ScaleBaseUtils","--fatmat="+fatmat+"----year="+year+"--month="+month+"--day="+day+"--h="+h+"--m="+m+"--s="+s+"\n"+result);

        return result;
    }



    public static String jianrongBirthdayStr(String bir){
        if (TextUtils.isEmpty(bir)){
            return "";
        }

        boolean isF1=judgingTimeFormat(bir,"yyyy-MM-dd");
        if (isF1){
            return bir;
        }

        boolean isF2=judgingTimeFormat(bir,"yyyy年MM月dd日");
        if (isF2){
            return formatTime3(parseTimeFormatter(bir,"yyyy年MM月dd日").getTime());
        }

        boolean isF3=judgingTimeFormat(bir,"yyyy年M月d日");
        if (isF3){
            return formatTime3(parseTimeFormatter(bir,"yyyy年M月d日").getTime());
        }


        //1978年1月1日

        return bir;
    }

    public static String getCurrentTime8(){
        SimpleDateFormat formatter3=new SimpleDateFormat("yyyy.MM.dd");
        curDate.setTime(System.currentTimeMillis());
        return formatter3.format(curDate);
    }

}
